package com.lute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.json.SummaryStatusJSON;
import com.lute.model.Approver;
import com.lute.model.User;
import com.lute.model.Client;
import com.lute.model.ClientAccountingPeriod;
import com.lute.model.ClientProfile;
import com.lute.model.Employee;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.UserEntitlement;
import com.lute.utils.ServerErrorResponse;

@Controller
public class ApproverSummaryScreenServlet {
	
	@RequestMapping (value="/approverSummaryScreen",method= RequestMethod.GET)
	public @ResponseBody String clientAdminSummaryScreen(HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String role = (String)session.getAttribute("role"); 
		
		/* check  authorization */
		if(!(role.equals("approver"))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		
		Approver approver = Approver.getApproverWithAllFromDB("id_user", sessionUserId);
		
		try {
			
			List<User> users = (List)approver.getEmployees();
			users.add(approver);
			List<EmployeeAccountingPeriod> employeeAccountinPeriods = EmployeeAccountingPeriod.
																			getCurrentEmployeeAccPeriods(users);	
			SummaryStatusJSON approverStatus = new SummaryStatusJSON();
			approverStatus = EmployeeAccountingPeriod.prepareSummaryStatus(employeeAccountinPeriods, approverStatus, role);
			
			jsonRes.put("checkedByApproverCount", approverStatus.getCheckedByApproverCount());
			jsonRes.put("checkedByClientAdminCount", approverStatus.getCheckedByClientAdminCount());
			jsonRes.put("initialCount", approverStatus.getInitialCount());
			jsonRes.put("rejectedByApproverCount", approverStatus.getRejectedByApproverCount());
			jsonRes.put("rejectedByClientAdminCount", approverStatus.getRejectedByClientAdminCount());
			jsonRes.put("sentToApproverCount", approverStatus.getSentToApproverCount());
			jsonRes.put("sentToClientAdminCount", approverStatus.getSentToClientAdminCount());
			jsonRes.put("approverAndHisEmployeesCount", Integer.toString((approver.getEmployees().size())));
		
		} catch(NullPointerException npe) {
			jsonRes.put("result", ServerErrorResponse.INTERNAL_ERROR.toString());
			result = jsonRes.toString();
			return result;
		}
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		return result;
	}
}
